//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
import java.util.*

class Qipan {
    var turnCount = 1
    var array = Array(9) { Array(9) { "+" } }

    fun zero() {
        for (i in 0 until 9) {
            for (j in 0 until 9) {
                array[i][j] = "+"
            }
        }
    }

    fun print() {
        for (i in 0 until 9) {
            for (j in 0 until 9) {
                print("${array[i][j]}  ")
            }
            println()
        }
    }

    fun luozi() {
        val scanner = Scanner(System.`in`)
        if (turnCount % 2 == 1) {
            println("到黑方落子")
        } else {
            println("到白方落子")
        }
        print("请选择落子地方，用空格分隔:")
        val input = scanner.nextLine()
        val numbers = input.split(" ")
        if (numbers.size != 2) {
            println("输入格式不正确，请输入两个数字并用空格隔开。")
            return
        }
        val i = numbers[0].toInt() - 1
        val j = numbers[1].toInt() - 1
        if (i < 0 || i >= 9 || j < 0 || j >= 9) {
            println("坐标超出范围，请重新输入。")
            return
        }
        array[i][j] = if (turnCount % 2 == 1) "⭕" else "●"
        turnCount++
    }

    fun victory() {
        for (i in 0 until 9) {
            for (j in 0 until 9) {
                if (array[i][j] == "⭕" || array[i][j] == "●") {
                    for (x in -1..1) {
                        for (y in -1..1) {
                            if (x != 0 || y != 0) {
                                if (i + 2 * x in 0..8 && j + 2 * y in 0..8) {
                                    if (array[i + x][j + y] == array[i][j] &&
                                        array[i + 2 * x][j + 2 * y] == array[i][j]
                                    ) {
                                        println("游戏结束，${if (array[i][j] == "⭕") "黑棋" else "白棋"}胜利")
                                        print()
                                        System.exit(0)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

fun main() {
    val game = Qipan()
    game.zero()
    while (true) {
        game.print()
        game.luozi()
        game.victory()
    }
}